package com.gzhryc.shared_device.oem.micro.queue;

import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.event.DelayHandleService;
import com.gzhryc.common.event.QuartzServer;
import com.gzhryc.common.kafka.KafkaConsumerFactory;
import com.gzhryc.common.kafka.KafkaProducerFactory;
import com.gzhryc.common.kafka.consumer.SingleKafkaConsumer;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.common.redis.RedisTools;
import com.gzhryc.jdbc.ConnectionFactory;
import com.gzhryc.jdbc.models.ConnectInfo;
import com.gzhryc.shared_device.common.BusyThreadPool;
import com.gzhryc.shared_device.common.CountryAreaUtils;
import com.gzhryc.shared_device.oem.code.BaseConstants;
import com.gzhryc.shared_device.oem.code.KafkaEventConstants;
import com.gzhryc.shared_device.oem.code.PayServiceFactory;
import com.gzhryc.shared_device.oem.micro.queue.handlers.MemberRechargeFinishEventHandler;
import com.gzhryc.shared_device.oem.micro.queue.handlers.order.OrderCancelEventHandler;
import com.gzhryc.shared_device.oem.micro.queue.handlers.order.OrderCreateEventHandler;
import com.gzhryc.shared_device.oem.micro.queue.handlers.UserAutoTransferMoneyEventHandler;
import com.gzhryc.shared_device.oem.micro.queue.handlers.order.OrderFinishEventHandler;
import com.gzhryc.shared_device.oem.micro.queue.handlers.order.OrderRefundEventHandler;
import com.gzhryc.shared_device.oem.micro.queue.handlers.profit.UserProfitCancelEventHandler;
import com.gzhryc.shared_device.oem.micro.queue.handlers.profit.UserProfitDivideEventHandler;
import com.gzhryc.shared_device.oem.micro.queue.times.*;
import org.apache.log4j.xml.DOMConfigurator;

import java.io.*;
import java.util.Properties;

public class Main {

	static Logger log = Logger.getLogger(Main.class);

	public static void main(String[] args) {
		try {
			String path = "config";
			if (args != null && args.length > 0) {
				path = args[0];
			}

			// 获取当前程序路径
			File logCfgFile = new File(path + "/log4j.xml");
			if (logCfgFile.exists()) {
				DOMConfigurator.configure(logCfgFile.getPath());
				log.info("加载" + path + "/log4j.xml日志配置信息");
			} else {
				DOMConfigurator.configure(Main.class.getClassLoader().getResource("log4j.xml"));
				log.info("加载默认log4j.xml日志配置信息");
			}

			File infoCfgFile = new File(path + "/info.properties");
			if (infoCfgFile.exists()) {
				PropertyTools.load(infoCfgFile);
				log.info("加载" + path + "/info.properties配置信息");
			} else {
				PropertyTools.load(Main.class.getClassLoader().getResourceAsStream("info.properties"));
				log.info("加载默认info.properties配置信息");
			}

			// Redis服务
			File redisCfgFile = new File(path + "/redis.properties");
			if (redisCfgFile.exists()) {
				PropertyTools propertyTools = new PropertyTools(redisCfgFile);
				RedisTools.get().initial(propertyTools.getProperties());
				log.info("加载" + path + "/redis.properties配置信息");
			} else {
				PropertyTools propertyTools = new PropertyTools(Main.class.getClassLoader().getResourceAsStream("redis.properties"));
				RedisTools.get().initial(propertyTools.getProperties());
				log.info("加载默认redis.properties配置信息");
			}

			ConnectInfo info = null;
			File jdbcCfgFile = new File(path + "/jdbc.properties");
			if (jdbcCfgFile.exists()) {
				Properties properties = new Properties();
				InputStream inStream = new FileInputStream(jdbcCfgFile);
				BufferedReader bf = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
				properties.load(bf);
				info = new ConnectInfo(properties);
				ConnectionFactory.addDataSource(BaseConstants.JDBC_KEY, info);
				ConnectionFactory.setDefaultJdbcKey(BaseConstants.JDBC_KEY);
				log.info("加载" + path + "/jdbc.properties数据库连接配置信息");
			} else {
				Properties properties = new Properties();
				properties.load(Main.class.getClassLoader().getResourceAsStream("jdbc.properties"));
				info = new ConnectInfo(properties);
				ConnectionFactory.addDataSource(BaseConstants.JDBC_KEY, info);
				ConnectionFactory.setDefaultJdbcKey(BaseConstants.JDBC_KEY);
				log.info("加载默认jdbc.properties数据库连接配置信息");
			}

			File kafkaCfgFile = new File(path + "/kafka_consumer.properties");
			if (kafkaCfgFile.exists()) {
				PropertyTools propertyTools = new PropertyTools(kafkaCfgFile);
				KafkaConsumerFactory.self().initial(propertyTools.getProperties());
				log.info("消息消费者加载" + path + "/kafka_consumer.properties配置信息");
			} else {
				log.info("未加载kafka_consumer.properties配置信息");
			}

			kafkaCfgFile = new File(path + "/kafka_producer.properties");
			if (kafkaCfgFile.exists()) {
				PropertyTools propertyTools = new PropertyTools(kafkaCfgFile);
				KafkaProducerFactory.self().initial(propertyTools.getProperties());
				log.info("消息生产者加载" + path + "/kafka_producer.properties配置信息");
			} else {
				log.info("未加载kafka_producer.properties配置信息");
			}

			PayServiceFactory.self().initial(PropertyTools.get().getString("httpDomain"));

			BusyThreadPool.self().initial();

			//初始化地区信息
			CountryAreaUtils.initial();

			DelayHandleService.get().run();    //启动延迟执行
			DelayHandleService.get().addHandler(new OrderCreateEventHandler());
			DelayHandleService.get().addHandler(new OrderFinishEventHandler());
			DelayHandleService.get().addHandler(new OrderCancelEventHandler());
			DelayHandleService.get().addHandler(new OrderRefundEventHandler());

			DelayHandleService.get().addHandler(new UserProfitDivideEventHandler());
			DelayHandleService.get().addHandler(new UserProfitCancelEventHandler());

			DelayHandleService.get().addHandler(new UserAutoTransferMoneyEventHandler());
			DelayHandleService.get().addHandler(new MemberRechargeFinishEventHandler());

			SingleKafkaConsumer orderKafkaConsumer = new SingleKafkaConsumer(KafkaEventConstants.TOPIC_Order, MicroConstants.PROGRAM_TYPE + "_order",
					new OrderKafkaEventListener());
			KafkaConsumerFactory.self().addConsumer(orderKafkaConsumer);

			//定时执行
			QuartzServer.start();
			//每3分钟执行一次
			QuartzServer.delayedRunJob("0 */3 * * * ?", Every3MinutesCheckJob.class, true);
			//每15分钟执行一次
			QuartzServer.delayedRunJob("0 */10 * * * ?", Every10MinutesCheckJob.class, true);
			//每5小时执行一次
			QuartzServer.delayedRunJob("0 0 */5 * * ?", Every5HourCheckJob.class, true);
			//每天凌晨1点
			QuartzServer.delayedRunJob("0 0 1 * * ?", Morning1CheckJob.class, false);
			//每天凌晨3点
			QuartzServer.delayedRunJob("0 0 3 * * ?", Morning3CheckJob.class, false);

		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
}
